﻿using System;
using System.Collections.Generic;
using System.IO;

namespace VeryGoodSync.Provider
{
    internal class FileSyncProvider : ISyncProvider
    {
        private Dictionary<string, string> _syncFiles;

        public void Init(Dictionary<string, string> arguments)
        {
            _syncFiles = arguments;
        }

        public void Sync(string sourceDir)
        {
            foreach (var file in _syncFiles)
            {
                var source = Path.Combine(sourceDir, Path.GetFileName(file.Value));
                if (!File.Exists(source)) continue;
                var targetDir = Path.GetDirectoryName(file.Value) ?? throw new Exception("无法获取目标文件的文件夹位置");
                if (!Directory.Exists(targetDir)) Directory.CreateDirectory(targetDir);
                if (!File.Exists(file.Value))
                {
                    File.Copy(source, file.Value);
                    continue;
                }

                if (Ui.GetFileMd5(source) == Ui.GetFileMd5(file.Value)) continue;
                File.Delete(file.Value);
                File.Copy(source, file.Value);
            }
        }

        public void Kill()
        {
            
        }

        public string Help()
        {
            return "File(N)：目标文件，将从Source目录下获取该文件，然后同步";
        }
    }
}
